package hos.filex.delegate;


import android.content.DialogInterface;

import androidx.activity.ComponentActivity;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LifecycleOwner;

import hos.delegate.BaseDelegate;
import hos.intent.IntentAvailable;

/**
 * <p>Title: ImageDelegate </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-03-14 15:03
 */
public class OpenDelegate extends BaseDelegate {

    private FileResult.OnOpenResultListener mOnOpenResultListener;

    @NonNull
    public static OpenDelegate create(ComponentActivity activity) {
        return new OpenDelegate(activity);
    }

    @NonNull
    public static OpenDelegate create(Fragment fragment) {
        return new OpenDelegate(fragment);
    }

    protected OpenDelegate(@NonNull LifecycleOwner lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    public void onCreate(@NonNull LifecycleOwner owner) {
        openLocationSetting();
        openFile();
        openWps();
        openMarket();
        openUrl();
    }


    @Nullable
    private ActivityResultLauncher<Void> openLocationSetting;

    /**
     * 打开文件
     */
    @NonNull
    private ActivityResultLauncher<Void> openLocationSetting() {
        if (openLocationSetting != null) {
            return openLocationSetting;
        }
        return openLocationSetting = FileResultContracts.openLocationSetting(requireActivityResultCaller(), new ActivityResultCallback<Boolean>() {
            @Override
            public void onActivityResult(Boolean o) {
                if (mOnOpenResultListener != null) {
                    mOnOpenResultListener.onResult(IntentAvailable.locationAvailable(requireActivity()));
                }
            }
        });
    }

    @Nullable
    private ActivityResultLauncher<String> openFile;

    /**
     * 打开文件
     */
    @NonNull
    private ActivityResultLauncher<String> openFile() {
        if (openFile != null) {
            return openFile;
        }
        return openFile = FileResultContracts.openFile(requireActivityResultCaller(), new ActivityResultCallback<Boolean>() {
            @Override
            public void onActivityResult(Boolean o) {
                if (mOnOpenResultListener != null) {
                    mOnOpenResultListener.onResult(o);
                }
            }
        });
    }

    @Nullable
    private ActivityResultLauncher<String> openWps;

    /**
     * 打开WPS
     */
    @NonNull
    private ActivityResultLauncher<String> openWps() {
        if (openWps != null) {
            return openWps;
        }
        return openWps = FileResultContracts.openWps(requireActivityResultCaller(), new ActivityResultCallback<Boolean>() {
            @Override
            public void onActivityResult(Boolean o) {
                if (mOnOpenResultListener != null) {
                    mOnOpenResultListener.onResult(o);
                }
            }
        });
    }

    @Nullable
    private ActivityResultLauncher<String> openMarket;

    /**
     * 打开应用市场
     */
    @NonNull
    private ActivityResultLauncher<String> openMarket() {
        if (openMarket != null) {
            return openMarket;
        }
        return openMarket = FileResultContracts.openMarket(requireActivityResultCaller(), new ActivityResultCallback<Boolean>() {
            @Override
            public void onActivityResult(Boolean o) {
                if (mOnOpenResultListener != null) {
                    mOnOpenResultListener.onResult(o);
                }
            }
        });
    }

    @Nullable
    private ActivityResultLauncher<String> openUrl;

    /**
     * 打开网页
     */
    @NonNull
    private ActivityResultLauncher<String> openUrl() {
        if (openUrl != null) {
            return openUrl;
        }
        return openUrl = FileResultContracts.openUrl(requireActivityResultCaller(), new ActivityResultCallback<Boolean>() {
            @Override
            public void onActivityResult(Boolean o) {
                if (mOnOpenResultListener != null) {
                    mOnOpenResultListener.onResult(o);
                }
            }
        });
    }

    //<editor-fold desc="Open">
    public void checkLocationSetting(FileResult.OnOpenResultListener listener) {
        if (IntentAvailable.locationAvailable(requireActivity())) {
            listener.onResult(true);
            return;
        }
        new AlertDialog.Builder(requireActivity())
                .setTitle("温馨提示")
                .setMessage("需要打开系统定位开关")
                .setCancelable(false)
                .setPositiveButton("去设置", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        openLocationSetting(listener);
                    }
                })
                .setNegativeButton("暂不开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        listener.onResult(false);
                    }
                }).show();
    }

    public void openLocationSetting(FileResult.OnOpenResultListener listener) {
        if (IntentAvailable.locationAvailable(requireActivity())) {
            listener.onResult(true);
            return;
        }
        mOnOpenResultListener = listener;
        try {
            openLocationSetting().launch(null);
        } catch (Exception e) {
            e.printStackTrace();
            if (listener != null) {
                listener.onResult(false);
            }
        }
    }

    public void openFile(String filePath, FileResult.OnOpenResultListener listener) {
        mOnOpenResultListener = listener;
        try {
            openFile().launch(filePath);
        } catch (Exception e) {
            e.printStackTrace();
            if (listener != null) {
                listener.onResult(false);
            }
        }
    }

    public void openWps(String filePath, FileResult.OnOpenResultListener listener) {
        mOnOpenResultListener = listener;
        try {
            openWps().launch(filePath);
        } catch (Exception e) {
            e.printStackTrace();
            if (listener != null) {
                listener.onResult(false);
            }
        }
    }

    public void openUrl(String url, FileResult.OnOpenResultListener listener) {
        mOnOpenResultListener = listener;
        try {
            openUrl().launch(url);
        } catch (Exception e) {
            e.printStackTrace();
            if (listener != null) {
                listener.onResult(false);
            }
        }
    }

    public void openMarket(String packageName, FileResult.OnOpenResultListener listener) {
        mOnOpenResultListener = listener;
        try {
            openMarket().launch(packageName);
        } catch (Exception e) {
            e.printStackTrace();
            if (listener != null) {
                listener.onResult(false);
            }
        }
    }

    public void openFile(String filePath) {
        openFile(filePath, null);
    }

    public void openWps(String filePath) {
        openWps(filePath, null);
    }

    public void openUrl(String url) {
        openUrl(url, null);
    }

    public void openMarket(String packageName) {
        openMarket(packageName, null);
    }
    //</editor-fold>


    @Override
    public void onDestroy(@NonNull LifecycleOwner owner) {
        mOnOpenResultListener = null;
        openFile = null;
        openWps = null;
        openUrl = null;
        openMarket = null;
        super.onDestroy(owner);
    }

}
